package com.ckzp.jfinal.system.controller;

import com.ckzp.core.id.UUIDHex;
import com.ckzp.jfinal.base.PageSearch;
import com.ckzp.jfinal.form.FormSelect;
import com.ckzp.jfinal.system.cache.SysDataCache;
import com.ckzp.plugins.layui.LayuiData;
import com.ckzp.core.json.JsonResult;
import com.ckzp.core.model.Datas;
import com.ckzp.core.util.DateUtil;
import com.ckzp.jfinal.base.BaseController;
import com.ckzp.jfinal.system.cache.SysTypeCache;
import com.ckzp.jfinal.form.FormData;
import com.ckzp.jfinal.interceptor.Admins;
import com.ckzp.jfinal.plugins.UpImage;
import com.ckzp.jfinal.system.model.SysApp;
import com.ckzp.jfinal.system.validate.SysAppValidator;
import com.jfinal.aop.Before;
import com.ckzp.jfinal.system.cache.SysTypeCache;
import com.ckzp.jfinal.plugins.tree.TreeNode;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;
import java.util.List;

/**
 * Created by Wendy(51594) on 2023-02-24 10:22:34.
 */
public class SysAppController extends BaseController {

    @Admins(roles = "login", name = "app接口验证默认", log = false)
    public void index() {
        renderText("app接口验证默认");
    }


    /**
     * app接口验证列表查询页面
     */
    @Admins(roles = "system", name = "app接口验证列表查询", log = false)
    public void SysAppList() {
        FormData fd = FormData.dao();
        TreeNode tree = TreeNode.newNode("page1").setTitle("测试分类").setSpread(true).setTurn(false);
        tree = SysTypeCache.n().getTypeDetailTree(tree, "test_type");
        fd.addData("tree", toJsonStr(TreeNode.newNodeList(tree)));
        fd.addData("def_id", tree.getDatas().get("def_id"));
        fd.addData("def_title", tree.getDatas().get("def_title"));

        setAttr("form", fd);
        render("SysAppList.html");
    }

    /**
     * app接口验证列表数据查询API
     * fd.addSelect("", SysDataCache.getFormSelect(""));
     */
    @Admins(roles = "system", name = "app接口验证列表查询Api", log = false)
    public void SysAppListDataApi() {
        PageSearch ps = PageSearch.n().init(getRequest(), "sys_app");

        ps.orderby(getRequest(), "aid desc");
        Page<SysApp> pages = ps.paginate(SysApp.dao);
        List<SysApp> datalist = pages.getList();
        for (SysApp app : datalist) {

        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }

    /**
     * 新增app接口验证Page
     */
    @Admins(roles = "system", name = "新增app接口验证页面", log = false)
    public void AddSysApp() {
        FormData fd = FormData.dao().setActions("ADD");
        setAttr("app", new SysApp().set("app_secret", UUIDHex.getUUIDHex()));
        setAttr("form", fd);
        render("ModifySysApp.html");
    }

    /**
     * 修改app接口验证页面
     * fd.addSelect("", SysDataCache.getFormSelect("").setLayFilter("").showHtml(o.mod.getStr("")));
     */
    @Admins(roles = "system", name = "修改app接口验证页面", log = false)
    public void EditSysApp() {
        String aid = getPara("aid", "");
        SysApp o = SysApp.dao.findById(aid);
        FormData fd = FormData.dao().setActions("EDIT");
        setAttr("app", new SysApp());
        if (o != null) {
            o.put("c_date_str", DateUtil.getDateTime(o.mod.getDate("c_date")));

        }
        setAttr("form", fd);
        setAttr("app", o);
        render("ModifySysApp.html");
    }

    /**
     * 修改app接口验证Api
     */
    @Before(SysAppValidator.class)
    @Admins(roles = "system", name = "修改app接口验证Api", log = true)
    public void ModifySysAppApi() {
//取得数据
        Datas data = getDatas();
        String aid = data.getStr("aid");//主键
        String actions = data.getStr("actions");//action
        if ("EDIT".equals(actions)) {//修改
            boolean results = SysApp.dao.findById(aid)
                    .set("app_name", data.getStr("app_name"))
                    .set("app_id", data.getStr("app_id"))
                    .set("app_secret", data.getStr("app_secret"))
                    .set("app_desc", data.getStr("app_desc"))
                    .set("status", data.getInt("status"))
                    .update();
            renderJson(Json(results, "修改"));
        } else {//添加
            boolean results = new SysApp()
                    .set("app_name", data.getStr("app_name"))
                    .set("app_id", data.getStr("app_id"))
                    .set("app_secret", data.getStr("app_secret"))
                    .set("c_date", new Date())
                    .set("app_desc", data.getStr("app_desc"))
                    .set("status", data.getInt("status"))
                    .save();
            renderJson(Json(results, "添加"));
        }
    }


    /**
     * 重新生成app_secretApi
     */
    @Admins(roles = "system", name = "重新生成app_secretApi", log = false)
    public void InitAppSecretApi() {
        renderJson(JsonResult.n().setData(UUIDHex.getUUIDHex()));
    }

    /**
     * 修改app接口验证字段Api
     * demo:
     * fd.addSelect("", SysDataCache.getFormSelect("").setLayFilter("").showHtml(o.mod.getStr("")));
     */
    @Admins(roles = "system", name = "修改app接口验证字段Api", log = true)
    public void ModifySysAppFieldApi() {
        String aid = getParaStr("key_id", "");
        String field = getPara("field", "");
        String value = getPara("values", "");
        SysApp o = SysApp.dao.findById(aid);
        if (o != null) {
            boolean status = o.set(field, value).update();
            renderJson(JsonResult.n(status ? 0 : 9, "修改" + (status ? "成功" : "失败")));
        } else {
            renderJson(JsonResult.n(0, "修改成功"));
        }
    }


    /**
     * 删除app接口验证Api
     */
    @Admins(roles = "system", name = "删除app接口验证Api", log = true)
    public void DelSysAppApi() {
        int aid = getInt("aid", 0);
        SysApp app = SysApp.dao.findById(aid);
        if (app == null) {
            renderJson(JsonResult.n(9, "记录不存在"));
        } else if (app.delete()) {
            renderJson(JsonResult.n(0, "删除成功"));
        } else {
            renderJson(JsonResult.n(9, "删除失败"));
        }
///////////////////////////////////
    }


    /**
     * 批量删除app接口验证Api
     */
    @Admins(roles = "system", name = "批量删除app接口验证Api", log = true)
    public void DelMoreSysAppApi() {
        renderJson(DelMoreModel(SysApp.dao, getParaStr("keys", ""), ""));
    }

    /**
     * app接口验证图片上传接口
     */
    @Admins(roles = "system", name = "app接口验证图片上传Api", log = false)
    public void UploadImages() {
        renderJson(UpImage.n().upload(getRequest(), "show", true, true));
    }


    /**
     * 改变app接口验证字段状态Api(0 or 1)
     */
    @Admins(roles = "system", name = "改变app接口验证状态Api", log = true)
    public void ChangeSysAppTagApi() {
        String keyid = getParaStr("key", "");
        String field = getParaStr("field", "");
        String msg = getParaStr("msg", "");
        renderJson(DoBasePub(SysApp.dao, keyid, field, msg));
    }
}